package com.kjc.demo.rcservice.app;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.TypeReference;
import com.kjc.demo.rcservice.bean.DocCategoryBean;
import com.kjc.demo.rcservice.dto.DocCategoryDTO;
import com.kjc.demo.rcservice.dto.req.DocCategoryReqDTO;
import com.kjc.demo.rcservice.dto.res.DocCategoryResDTO;
import com.kjc.demo.rcservice.service.DocCategoryService;
import com.sdhs.paas.sdk.base.common.BasePage;
import com.sdhs.paas.sdk.base.common.entity.PaginationEntity;
import com.sdhs.paas.sdk.base.dto.PageInfoDTO;
import com.sdhs.paas.sdk.base.utils.EntityDtoConvertUtil;
import com.sdhs.paas.sdk.core.PaasBaseRequest;
import com.sdhs.paas.sdk.core.PaasBaseResponse;
import com.sdhs.paas.sdk.core.RefreshableRestController;

/**
 * @author DocCategory
 * @version 1.0
 * @Title:  代码生成接口，用于接受前端传输过来的数据
 * @author kangjichang
 * @date 2018-12-07
 */
 
@RefreshableRestController
@RequestMapping(value = "/docCategory")
public class DocCategoryController {

    @Autowired
    private DocCategoryService docCategoryService;
    
    /**
     * 分页查询接口
     * */
    @PostMapping("/getDocCategoryPageList")
	public PaasBaseResponse<DocCategoryResDTO> getDocCategoryPageList(@RequestBody PaasBaseRequest<DocCategoryReqDTO> docCategoryReqDTO) {
    	DocCategoryDTO docCategoryDTO = docCategoryReqDTO.getBody().getDocCategoryDTO();
    	PageInfoDTO pageInfoDTO = docCategoryReqDTO.getBody().getPageInfo();
    	
		BasePage pageRequest = new BasePage();
		pageRequest.setPage(pageInfoDTO.getPage());
		pageRequest.setCount(pageInfoDTO.getPageSize());
		
		DocCategoryBean docCategoryBean = EntityDtoConvertUtil.convertToEntity(docCategoryDTO, DocCategoryBean.class);
		PaginationEntity<DocCategoryBean> page = docCategoryService.getPageDocCategoryList(pageRequest, docCategoryBean);
    	PageInfoDTO resPageInfoDTO = new PageInfoDTO();
    	DocCategoryResDTO docCategoryResDTO = new DocCategoryResDTO();
		resPageInfoDTO.setRowCount(page.getTotal());
		docCategoryResDTO.setPageInfo(resPageInfoDTO);
		List<DocCategoryBean> docCategoryBeanList = page.getRows();
		List<DocCategoryDTO> dtoList = new ArrayList<DocCategoryDTO>();
		dtoList = EntityDtoConvertUtil.convertToDTOList(docCategoryBeanList, new TypeReference<List<DocCategoryDTO>>() {
		});
		docCategoryResDTO.setDocCategoryDTOList(dtoList);
    	return new PaasBaseResponse<>(docCategoryResDTO);
	}
	
	/**
	 * 保存接口
	 * */
	@PostMapping("/saveDocCategory")
	public PaasBaseResponse<DocCategoryResDTO> saveDocCategory(@RequestBody PaasBaseRequest<DocCategoryReqDTO> docCategoryReqDTO) {
		DocCategoryDTO docCategoryDTO = docCategoryReqDTO.getBody().getDocCategoryDTO();
		DocCategoryBean docCategoryBean = EntityDtoConvertUtil.convertToEntity(docCategoryDTO, DocCategoryBean.class);
		String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
		docCategoryBean.setId(uuid);
		Boolean flag = docCategoryService.saveDocCategory(docCategoryBean);
		docCategoryDTO.setId(uuid);
		DocCategoryResDTO docCategoryResDTO = new DocCategoryResDTO();
		docCategoryResDTO.setDocCategoryDTO(docCategoryDTO);
		return new PaasBaseResponse<>(docCategoryResDTO);
	}
	
	/**
	 * 修改接口
	 * */
	@PostMapping("/updateDocCategory")
	public PaasBaseResponse<DocCategoryResDTO> updateDocCategory(@RequestBody PaasBaseRequest<DocCategoryReqDTO> docCategoryReqDTO) {
		DocCategoryDTO docCategoryDTO = docCategoryReqDTO.getBody().getDocCategoryDTO();
		DocCategoryBean docCategoryBean = EntityDtoConvertUtil.convertToEntity(docCategoryDTO, DocCategoryBean.class);
		Boolean flag = docCategoryService.updateDocCategory(docCategoryBean);
		DocCategoryResDTO docCategoryResDTO = new DocCategoryResDTO();
		docCategoryResDTO.setDocCategoryDTO(docCategoryDTO);
		return new PaasBaseResponse<>(docCategoryResDTO);
	}
	
	/**
	 * 获取详情接口
	 * */
	@PostMapping("/getDocCategoryDetail")
	public PaasBaseResponse<DocCategoryResDTO> getDocCategoryDetail(@RequestBody PaasBaseRequest<DocCategoryReqDTO> docCategoryReqDTO) {
		DocCategoryDTO docCategoryDTO = docCategoryReqDTO.getBody().getDocCategoryDTO();
		DocCategoryBean docCategoryBean = EntityDtoConvertUtil.convertToEntity(docCategoryDTO, DocCategoryBean.class);
		docCategoryBean = docCategoryService.getDocCategoryDetail(docCategoryBean.getId());
		DocCategoryResDTO docCategoryResDTO = new DocCategoryResDTO();
		docCategoryResDTO.setDocCategoryDTO(docCategoryDTO);
		return new PaasBaseResponse<>(docCategoryResDTO);
	}
	
	/**
	 * 删除接口
	 * */
	@PostMapping("/deleteDocCategory")
	public PaasBaseResponse<DocCategoryResDTO> deleteDocCategory(@RequestBody PaasBaseRequest<DocCategoryReqDTO> docCategoryReqDTO) {
		DocCategoryDTO docCategoryDTO = docCategoryReqDTO.getBody().getDocCategoryDTO();
		DocCategoryBean docCategoryBean = EntityDtoConvertUtil.convertToEntity(docCategoryDTO, DocCategoryBean.class);
		Boolean flag = docCategoryService.deleteDocCategory(docCategoryBean);
		DocCategoryResDTO docCategoryResDTO = new DocCategoryResDTO();
		docCategoryResDTO.setDocCategoryDTO(docCategoryDTO);
		return new PaasBaseResponse<>(docCategoryResDTO);
	}
}
